﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
namespace PrintDrawing
{
    /// <summary>
    /// Interaction logic for ConfigPrint.xaml
    /// </summary>
    public partial class ConfigPrint : UserControl
    {
        public event OpenClick onOpenClick;
        Popup parent;
        BaseClassDraw.PrintInfo printInfo=new BaseClassDraw.PrintInfo();
        BaseClassDraw.PageInfo pageInfo = new BaseClassDraw.PageInfo();
        BaseClassDraw.TYPE_PRINT _type_print=BaseClassDraw.TYPE_PRINT.A4;
        bool _setProperty = true;
        public ConfigPrint(Popup p)
        {
            InitializeComponent();
            parent = p;
            rdoA0.Checked+=new RoutedEventHandler(rdo_Checked);
            rdoA1.Checked += new RoutedEventHandler(rdo_Checked);
            rdoA2.Checked += new RoutedEventHandler(rdo_Checked);
            rdoA3.Checked += new RoutedEventHandler(rdo_Checked);
            rdoA4.Checked += new RoutedEventHandler(rdo_Checked);
            rdoCustom.Checked += new RoutedEventHandler(rdo_Checked);
            rdoLandscape.Checked += new RoutedEventHandler(SwitchSize);
            rdoPortrait.Checked += new RoutedEventHandler(SwitchSize);
        }

        public void SetProperty(BaseClassDraw.PrintInfo p)
        {            
            switch (p.TypePrint)
	        {
                case PrintDrawing.BaseClassDraw.TYPE_PRINT.A0:
                    rdoA0.IsChecked=true;
                break;
                case PrintDrawing.BaseClassDraw.TYPE_PRINT.A1:
                    rdoA1.IsChecked=true;
                break;
                case PrintDrawing.BaseClassDraw.TYPE_PRINT.A2:
                    rdoA2.IsChecked=true;
                break;
                case PrintDrawing.BaseClassDraw.TYPE_PRINT.A3:
                    rdoA3.IsChecked=true;
                break;
                case PrintDrawing.BaseClassDraw.TYPE_PRINT.A4:
                    rdoA4.IsChecked=true;
                break;
                case PrintDrawing.BaseClassDraw.TYPE_PRINT.Custom:
                    rdoCustom.IsChecked=true;
                break;
                default:
                break;
            }
            txtHeight.Text=Math.Round(p.Height,0).ToString();
            txtWidth.Text=Math.Round(p.Width,0).ToString();
            txtTitel.Text=p.TitelPrint;        
            if (rdoCustom.IsChecked == true)
            {
                txtHeight.IsEnabled = true;
                txtWidth.IsEnabled = true;
            }
            else
            {
                txtHeight.IsEnabled = false;
                txtWidth.IsEnabled = false;
            }
            _setProperty = true;
            if (p.Orientation == "PORTRAINT")
            {
                rdoPortrait.IsChecked = true;
            }
            else
            {
                rdoLandscape.IsChecked = true;
            }
            printInfo = p;
            _setProperty = false;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (ckbApplyCurrentPage.IsChecked == true)
            {
                PrintDrawing.BaseClassDraw.PrintInfo p = new PrintDrawing.BaseClassDraw.PrintInfo();
                p.Height = double.Parse(txtHeight.Text);
                p.Width = double.Parse(txtWidth.Text);             
                onOpenClick(this, p, true);
                parent.IsOpen = false;
                return;
            }
            printInfo.Height=double.Parse(txtHeight.Text);
            printInfo.Width=double.Parse(txtWidth.Text);
            printInfo.TitelPrint=txtTitel.Text;
            printInfo.TypePrint=_type_print;            
            parent.IsOpen=false;
            if (onOpenClick != null)
            {
                onOpenClick(this, printInfo,false);
            }
            if (rdoPortrait.IsChecked == true)
            {
                printInfo.Orientation = "PORTRAINT";
            }
            else
            {
                printInfo.Orientation = "LANDSCAPE";
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            parent.IsOpen=false;
        }

        void setStatusInfo()
        {
            if (rdoA0.IsChecked == true)
            {
                _type_print = BaseClassDraw.TYPE_PRINT.A0;
                txtHeight.Text = "4849";
                txtWidth.Text = "3453";
            }
            else if (rdoA1.IsChecked == true)
            {
                _type_print = BaseClassDraw.TYPE_PRINT.A1;
                txtHeight.Text = "2439";
                txtWidth.Text = "3453";
            }
            else if (rdoA2.IsChecked == true)
            {
                _type_print = BaseClassDraw.TYPE_PRINT.A2;
                txtHeight.Text = "1724";
                txtWidth.Text = "2439";
            }
            else if (rdoA3.IsChecked == true)
            {
                _type_print = BaseClassDraw.TYPE_PRINT.A3;
                txtHeight.Text = "1219";
                txtWidth.Text = "1724";
            }
            else if (rdoA4.IsChecked == true)
            {
                _type_print = BaseClassDraw.TYPE_PRINT.A4;
                txtHeight.Text = "1219";
                txtWidth.Text = "862";
            }
            else if (rdoCustom.IsChecked == true)
            {
                _type_print = BaseClassDraw.TYPE_PRINT.Custom;
                txtHeight.Text = printInfo.Height.ToString();
                txtWidth.Text = printInfo.Width.ToString();
            }
            if (rdoCustom.IsChecked == true)
            {
                txtHeight.IsEnabled = true;
                txtWidth.IsEnabled = true;
                rdoLandscape.IsEnabled = false;
                rdoPortrait.IsEnabled = false;
            }
            else
            {
                txtHeight.IsEnabled = false;
                txtWidth.IsEnabled = false;
                rdoPortrait.IsEnabled = true;
                rdoLandscape.IsEnabled = true;
            }                       
            
        }

        private void SwitchSize(object sender, RoutedEventArgs e)
        {
            SwichSize();
        }

        private void SwichSize()
        {
            if (rdoLandscape.IsChecked == true & !_setProperty)
            {
                string tam = txtHeight.Text;
                txtHeight.Text = txtWidth.Text;
                txtWidth.Text = tam;
            }
            else if (rdoPortrait.IsChecked == true & !_setProperty)
            {
                setStatusInfo();
            }
        }

        private void rdo_Checked(object sender, RoutedEventArgs e)
        {
            setStatusInfo();
        }
    }
    public delegate void OpenClick(object sender, BaseClassDraw.PrintInfo e,bool a);
}
